<template>
  <div id="questionsView">
    <h2>浏览题目</h2>
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="题目名称">
        <a-input v-model="searchParams.title" placeholder="请输入名称" style="min-width: 240px" />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag v-model="searchParams.tags" placeholder="请输入标签" style="min-width: 240px" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="onSubmit">提交</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        total,
        pageSize: searchParams.pageSize,
        current: searchParams.current
      }"
      @page-change="onChangePage"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green">{{ tag }}</a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{ ` ${record.submitNum ? record.acceptedNum / record.submitNum : '0'} % ${record.acceptedNum}/${record.submitNum}` }}
      </template>
      <template #createTime="{ record }"> {{ moment(record.createTime).format('YYYY-MM-DD') }}</template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toQuestionPage(record)">做题</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, watchEffect } from 'vue';
import { Question, QuestionControllerService, QuestionQueryRequest } from '../../../generated';
import message from '@arco-design/web-vue/es/message';
import { useRouter } from 'vue-router';
import moment from 'moment';

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  title: '',
  tags: [],
  pageSize: 10,
  current: 1
});
const router = useRouter();

const columns = [
  {
    title: '题目号',
    dataIndex: 'id'
  },
  {
    title: '题目名称',
    dataIndex: 'title'
  },
  {
    title: '标签',
    slotName: 'tags'
  },
  {
    title: '通过率',
    slotName: 'acceptedRate'
  },

  {
    title: '创建时间',
    slotName: 'createTime'
  },
  {
    title: '',
    slotName: 'optional'
  }
];

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(searchParams.value);
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error('获取题目失败' + res.message);
  }
};

/**
 * todo watchEffect 可以监听这个里面所有的值 函数里面的值发生变化就会再次调用这个函数
 * 监听 searchParams
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时请求数据
 */
onMounted(() => {
  loadData();
});

const onChangePage = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page
  };
};

const toQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`
  });
};

/**
 * 搜索题目
 *
 * todo nb 这里采用修改 watchEffect监听的的数据，来发起重新请求
 */
const onSubmit = async () => {
  //这里需要重置页号
  searchParams.value = {
    ...searchParams.value,
    current: 1
  };
};
</script>

<style scoped>
#questionsView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
